home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / mpatrol / include / mpatrol.h
C/C++ Source or Header  |  2000-05-16  |  16KB  |  519 lines

  1. #ifndef MP_MPATROL_H
  2. #define MP_MPATROL_H
  3.  
  4.  
  5. /*
  6.  * mpatrol
  7.  * A library for controlling and tracing dynamic memory allocations.
  8.  * Copyright (C) 1997-2000 Graeme S. Roy <graeme@epc.co.uk>
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Library General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2 of the License, or (at your option) any later version.
  14.  *
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.  * Library General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU Library General Public
  21.  * License along with this library; if not, write to the Free
  22.  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23.  * MA 02111-1307, USA.
  24.  */
  25.  
  26.  
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30.  
  31. /* The version of the mpatrol library.  The version is of the format vrrff,
  32.  * where v represents the version number, rr represents the revision number
  33.  * and ff represents the bug fix count.
  34.  */
  35.  
  36. #define MPATROL_VERSION 10200
  37.  
  38.  
  39. /* A macro for representing constant function parameters.
  40.  */
  41.  
  42. #ifndef MP_CONST
  43. #ifdef __STDC__
  44. #define MP_CONST const
  45. #else /* __STDC__ */
  46. #define MP_CONST
  47. #endif /* __STDC__ */
  48. #endif /* MP_CONST */
  49.  
  50.  
  51. /* A macro for determining the current function name.
  52.  */
  53.  
  54. #ifndef MP_FUNCNAME
  55. #ifdef __GNUC__
  56. #define MP_FUNCNAME __PRETTY_FUNCTION__
  57. #else /* __GNUC__ */
  58. #define MP_FUNCNAME NULL
  59. #endif /* __GNUC__ */
  60. #endif /* MP_FUNCNAME */
  61.  
  62.  
  63. /* A macro for disabling the definition of replacement C++ operators.
  64.  */
  65.  
  66. #ifndef MP_NOCPLUSPLUS
  67. #define MP_NOCPLUSPLUS 0
  68. #endif /* MP_NOCPLUSPLUS */
  69.  
  70.  
  71. /* Options for backwards compatibility with other versions of mallopt().  They
  72.  * are all currently ignored as they have no meaning when used with mpatrol.
  73.  */
  74.  
  75. #ifdef M_MXFAST
  76. #undef M_MXFAST
  77. #endif /* M_MXFAST */
  78. #ifdef M_NLBLKS
  79. #undef M_NLBLKS
  80. #endif /* M_NLBLKS */
  81. #ifdef M_GRAIN
  82. #undef M_GRAIN
  83. #endif /* M_GRAIN */
  84. #ifdef M_KEEP
  85. #undef M_KEEP
  86. #endif /* M_KEEP */
  87.  
  88. #define M_MXFAST 1
  89. #define M_NLBLKS 2
  90. #define M_GRAIN  3
  91. #define M_KEEP   4
  92.  
  93.  
  94. /* Options that can be set using mallopt().  They all correspond to their
  95.  * environment variable option equivalent except for MP_OPT_SETFLAGS and
  96.  * MP_OPT_UNSETFLAGS.
  97.  */
  98.  
  99. #define MP_OPT_HELP          0
  100. #define MP_OPT_SETFLAGS      -1
  101. #define MP_OPT_UNSETFLAGS    -2
  102. #define MP_OPT_ALLOCSTOP     -3
  103. #define MP_OPT_REALLOCSTOP   -4
  104. #define MP_OPT_FREESTOP      -5
  105. #define MP_OPT_ALLOCBYTE     -6
  106. #define MP_OPT_FREEBYTE      -7
  107. #define MP_OPT_OFLOWBYTE     -8
  108. #define MP_OPT_OFLOWSIZE     -9
  109. #define MP_OPT_DEFALIGN      -10
  110. #define MP_OPT_LIMIT         -11
  111. #define MP_OPT_FAILFREQ      -12
  112. #define MP_OPT_FAILSEED      -13
  113. #define MP_OPT_UNFREEDABORT  -14
  114. #define MP_OPT_LOGFILE       -15
  115. #define MP_OPT_PROFFILE      -16
  116. #define MP_OPT_PROGFILE      -17
  117. #define MP_OPT_AUTOSAVE      -18
  118. #define MP_OPT_CHECK         -19
  119. #define MP_OPT_SMALLBOUND    -20
  120. #define MP_OPT_MEDIUMBOUND   -21
  121. #define MP_OPT_LARGEBOUND    -22
  122.  
  123.  
  124. /* Flags that can be set or unset using mallopt() and MP_OPT_SETFLAGS or
  125.  * MP_OPT_UNSETFLAGS respectively.  They all correspond to their environment
  126.  * variable option equivalent except for MP_FLG_PAGEALLOC and MP_FLG_ALLOCUPPER.
  127.  */
  128.  
  129. #define MP_FLG_SHOWALL       (MP_FLG_SHOWMAP | MP_FLG_SHOWSYMBOLS | \
  130.                               MP_FLG_SHOWFREED | MP_FLG_SHOWUNFREED)
  131. #define MP_FLG_SHOWMAP       0x00000001
  132. #define MP_FLG_SHOWSYMBOLS   0x00000002
  133. #define MP_FLG_SHOWFREED     0x00000004
  134. #define MP_FLG_SHOWUNFREED   0x00000008
  135. #define MP_FLG_LOGALL        (MP_FLG_LOGALLOCS | MP_FLG_LOGREALLOCS | \
  136.                               MP_FLG_LOGFREES | MP_FLG_LOGMEMORY)
  137. #define MP_FLG_LOGALLOCS     0x00000010
  138. #define MP_FLG_LOGREALLOCS   0x00000020
  139. #define MP_FLG_LOGFREES      0x00000040
  140. #define MP_FLG_LOGMEMORY     0x00000080
  141. #define MP_FLG_CHECKALL      (MP_FLG_CHECKALLOCS | MP_FLG_CHECKREALLOCS | \
  142.                               MP_FLG_CHECKFREES)
  143. #define MP_FLG_CHECKALLOCS   0x00000100
  144. #define MP_FLG_CHECKREALLOCS 0x00000200
  145. #define MP_FLG_CHECKFREES    0x00000400
  146. #define MP_FLG_PROF          0x00000800
  147. #define MP_FLG_SAFESIGNALS   0x00001000
  148. #define MP_FLG_NOPROTECT     0x00002000
  149. #define MP_FLG_NOFREE        0x00004000
  150. #define MP_FLG_PRESERVE      0x00008000
  151. #define MP_FLG_OFLOWWATCH    0x00010000
  152. #define MP_FLG_PAGEALLOC     0x00020000
  153. #define MP_FLG_ALLOCUPPER    0x00040000
  154. #define MP_FLG_USEMMAP       0x00080000
  155. #define MP_FLG_USEDEBUG      0x00100000
  156. #define MP_FLG_ALLOWOFLOW    0x00200000
  157.  
  158.  
  159. /* The different types of memory allocation and operation functions.
  160.  */
  161.  
  162. typedef enum __mp_alloctype
  163. {
  164.     MP_AT_MALLOC,    /* malloc() */
  165.     MP_AT_CALLOC,    /* calloc() */
  166.     MP_AT_MEMALIGN,  /* memalign() */
  167.     MP_AT_VALLOC,    /* valloc() */
  168.     MP_AT_PVALLOC,   /* pvalloc() */
  169.     MP_AT_STRDUP,    /* strdup() */
  170.     MP_AT_STRNDUP,   /* strndup() */
  171.     MP_AT_STRSAVE,   /* strsave() */
  172.     MP_AT_STRNSAVE,  /* strnsave() */
  173.     MP_AT_REALLOC,   /* realloc() */
  174.     MP_AT_RECALLOC,  /* recalloc() */
  175.     MP_AT_EXPAND,    /* expand() */
  176.     MP_AT_FREE,      /* free() */
  177.     MP_AT_CFREE,     /* cfree() */
  178.     MP_AT_NEW,       /* operator new */
  179.     MP_AT_NEWVEC,    /* operator new[] */
  180.     MP_AT_DELETE,    /* operator delete */
  181.     MP_AT_DELETEVEC, /* operator delete[] */
  182.     MP_AT_MEMSET,    /* memset() */
  183.     MP_AT_BZERO,     /* bzero() */
  184.     MP_AT_MEMCCPY,   /* memccpy() */
  185.     MP_AT_MEMCPY,    /* memcpy() */
  186.     MP_AT_MEMMOVE,   /* memmove() */
  187.     MP_AT_BCOPY,     /* bcopy() */
  188.     MP_AT_MEMCHR,    /* memchr() */
  189.     MP_AT_MEMMEM,    /* memmem() */
  190.     MP_AT_MEMCMP,    /* memcmp() */
  191.     MP_AT_BCMP,      /* bcmp() */
  192.     MP_AT_MAX
  193. }
  194. __mp_alloctype;
  195.  
  196.  
  197. /* The details of a single function in a call stack.
  198.  */
  199.  
  200. typedef struct __mp_allocstack
  201. {
  202.     struct __mp_allocstack *next; /* next address node in call stack */
  203.     char *name;                   /* name of function */
  204.     void *addr;                   /* return address in function */
  205. }
  206. __mp_allocstack;
  207.  
  208.  
  209. /* The details of a single memory allocation.
  210.  */
  211.  
  212. typedef struct __mp_allocinfo
  213. {
  214.     void *block;            /* pointer to block of memory */
  215.     size_t size;            /* size of block of memory */
  216.     __mp_alloctype type;    /* type of memory allocation */
  217.     unsigned long alloc;    /* allocation index */
  218.     unsigned long realloc;  /* reallocation index */
  219.     unsigned long thread;   /* thread identifier */
  220.     char *func;             /* calling function name */
  221.     char *file;             /* file name in which call took place */
  222.     unsigned long line;     /* line number at which call took place */
  223.     __mp_allocstack *stack; /* call stack details */
  224.     char freed;             /* allocation has been freed */
  225. }
  226. __mp_allocinfo;
  227.  
  228.  
  229. #ifndef NDEBUG
  230.  
  231. #ifdef malloc
  232. #undef malloc
  233. #endif /* malloc */
  234. #ifdef calloc
  235. #undef calloc
  236. #endif /* calloc */
  237. #ifdef memalign
  238. #undef memalign
  239. #endif /* memalign */
  240. #ifdef valloc
  241. #undef valloc
  242. #endif /* valloc */
  243. #ifdef pvalloc
  244. #undef pvalloc
  245. #endif /* pvalloc */
  246. #ifdef strdup
  247. #undef strdup
  248. #endif /* strdup */
  249. #ifdef strndup
  250. #undef strndup
  251. #endif /* strndup */
  252. #ifdef strsave
  253. #undef strsave
  254. #endif /* strsave */
  255. #ifdef strnsave
  256. #undef strnsave
  257. #endif /* strnsave */
  258. #ifdef realloc
  259. #undef realloc
  260. #endif /* realloc */
  261. #ifdef recalloc
  262. #undef recalloc
  263. #endif /* recalloc */
  264. #ifdef expand
  265. #undef expand
  266. #endif /* expand */
  267. #ifdef free
  268. #undef free
  269. #endif /* free */
  270. #ifdef cfree
  271. #undef cfree
  272. #endif /* cfree */
  273. #ifdef memset
  274. #undef memset
  275. #endif /* memset */
  276. #ifdef bzero
  277. #undef bzero
  278. #endif /* bzero */
  279. #ifdef memccpy
  280. #undef memccpy
  281. #endif /* memccpy */
  282. #ifdef memcpy
  283. #undef memcpy
  284. #endif /* memcpy */
  285. #ifdef memmove
  286. #undef memmove
  287. #endif /* memmove */
  288. #ifdef bcopy
  289. #undef bcopy
  290. #endif /* bcopy */
  291. #ifdef memchr
  292. #undef memchr
  293. #endif /* memchr */
  294. #ifdef memmem
  295. #undef memmem
  296. #endif /* memmem */
  297. #ifdef memcmp
  298. #undef memcmp
  299. #endif /* memcmp */
  300. #ifdef bcmp
  301. #undef bcmp
  302. #endif /* bcmp */
  303.  
  304. #if !MP_NOCPLUSPLUS
  305. #ifdef __cplusplus
  306. #ifdef new
  307. #undef new
  308. #endif /* new */
  309. #ifdef delete
  310. #undef delete
  311. #endif /* delete */
  312. #endif /* __cplusplus */
  313. #endif /* MP_NOCPLUSPLUS */
  314.  
  315.  
  316. #define malloc(l) __mp_alloc((l), 0, MP_AT_MALLOC, MP_FUNCNAME, __FILE__, \
  317.                              __LINE__, 0)
  318. #define calloc(l, n) __mp_alloc((l) * (n), 0, MP_AT_CALLOC, MP_FUNCNAME, \
  319.                                 __FILE__, __LINE__, 0)
  320. #define memalign(a, l) __mp_alloc((l), (a), MP_AT_MEMALIGN, MP_FUNCNAME, \
  321.                                   __FILE__, __LINE__, 0)
  322. #define valloc(l) __mp_alloc((l), 0, MP_AT_VALLOC, MP_FUNCNAME, __FILE__, \
  323.                              __LINE__, 0)
  324. #define pvalloc(l) __mp_alloc((l), 0, MP_AT_PVALLOC, MP_FUNCNAME, __FILE__, \
  325.                               __LINE__, 0)
  326. #define strdup(p) __mp_strdup((p), 0, MP_AT_STRDUP, MP_FUNCNAME, __FILE__, \
  327.                               __LINE__, 0)
  328. #define strndup(p, l) __mp_strdup((p), (l), MP_AT_STRNDUP, MP_FUNCNAME, \
  329.                                   __FILE__, __LINE__, 0)
  330. #define strsave(p) __mp_strdup((p), 0, MP_AT_STRSAVE, MP_FUNCNAME, __FILE__, \
  331.                                __LINE__, 0)
  332. #define strnsave(p, l) __mp_strdup((p), (l), MP_AT_STRNSAVE, MP_FUNCNAME, \
  333.                                    __FILE__, __LINE__, 0)
  334. #define realloc(p, l) __mp_realloc((p), (l), 0, MP_AT_REALLOC, MP_FUNCNAME, \
  335.                                    __FILE__, __LINE__, 0)
  336. #define recalloc(p, l, n) __mp_realloc((p), (l) * (n), 0, MP_AT_RECALLOC, \
  337.                                        MP_FUNCNAME, __FILE__, __LINE__, 0)
  338. #define expand(p, l) __mp_realloc((p), (l), 0, MP_AT_EXPAND, MP_FUNCNAME, \
  339.                                   __FILE__, __LINE__, 0)
  340. #define free(p) __mp_free((p), MP_AT_FREE, MP_FUNCNAME, __FILE__, __LINE__, 0)
  341. #define cfree(p, l, n) __mp_free((p), MP_AT_CFREE, MP_FUNCNAME, __FILE__, \
  342.                                  __LINE__, 0)
  343. #define memset(p, c, l) __mp_setmem((p), (l), (unsigned char) (c), \
  344.                                     MP_AT_MEMSET, MP_FUNCNAME, __FILE__, \
  345.                                     __LINE__, 0)
  346. #define bzero(p, l) (void) __mp_setmem((p), (l), 0, MP_AT_BZERO, MP_FUNCNAME, \
  347.                                        __FILE__, __LINE__, 0)
  348. #define memccpy(q, p, c, l) __mp_copymem((p), (q), (l), (unsigned char) (c), \
  349.                                          MP_AT_MEMCCPY, MP_FUNCNAME, __FILE__, \
  350.                                          __LINE__, 0)
  351. #define memcpy(q, p, l) __mp_copymem((p), (q), (l), 0, MP_AT_MEMCPY, \
  352.                                      MP_FUNCNAME, __FILE__, __LINE__, 0)
  353. #define memmove(q, p, l) __mp_copymem((p), (q), (l), 0, MP_AT_MEMMOVE, \
  354.                                       MP_FUNCNAME, __FILE__, __LINE__, 0)
  355. #define bcopy(p, q, l) (void) __mp_copymem((p), (q), (l), 0, MP_AT_BCOPY, \
  356.                                            MP_FUNCNAME, __FILE__, __LINE__, 0)
  357. #define memchr(p, c, l) __mp_locatemem((p), (l), NULL, (size_t) (c), \
  358.                                        MP_AT_MEMCHR, MP_FUNCNAME, __FILE__, \
  359.                                        __LINE__, 0)
  360. #define memmem(p, l, q, m) __mp_locatemem((p), (l), (q), (m), MP_AT_MEMMEM, \
  361.                                           MP_FUNCNAME, __FILE__, __LINE__, 0)
  362. #define memcmp(p, q, l) __mp_comparemem((p), (q), (l), MP_AT_MEMCMP, \
  363.                                         MP_FUNCNAME, __FILE__, __LINE__, 0)
  364. #define bcmp(p, q, l) __mp_comparemem((p), (q), (l), MP_AT_BCMP, MP_FUNCNAME, \
  365.                                       __FILE__, __LINE__, 0)
  366.  
  367.  
  368. #ifdef __cplusplus
  369. extern "C"
  370. {
  371. #endif /* __cplusplus */
  372.  
  373.  
  374. void __mp_init(void);
  375. void __mp_fini(void);
  376. void *__mp_alloc(size_t, size_t, __mp_alloctype, MP_CONST char *,
  377.                  MP_CONST char *, unsigned long, size_t);
  378. char *__mp_strdup(MP_CONST char *, size_t, __mp_alloctype, MP_CONST char *,
  379.                   MP_CONST char *, unsigned long, size_t);
  380. void *__mp_realloc(void *, size_t, size_t, __mp_alloctype, MP_CONST char *,
  381.                    MP_CONST char *, unsigned long, size_t);
  382. void __mp_free(void *, __mp_alloctype, MP_CONST char *, MP_CONST char *,
  383.                unsigned long, size_t);
  384. void *__mp_setmem(void *, size_t, unsigned char, __mp_alloctype,
  385.                   MP_CONST char *, MP_CONST char *, unsigned long, size_t);
  386. void *__mp_copymem(MP_CONST void *, void *, size_t, unsigned char,
  387.                    __mp_alloctype, MP_CONST char *, MP_CONST char *,
  388.                    unsigned long, size_t);
  389. void *__mp_locatemem(MP_CONST void *, size_t, MP_CONST void *, size_t,
  390.                      __mp_alloctype, MP_CONST char *, MP_CONST char *,
  391.                      unsigned long, size_t);
  392. int __mp_comparemem(MP_CONST void *, MP_CONST void *, size_t, __mp_alloctype,
  393.                     MP_CONST char *, MP_CONST char *, unsigned long, size_t);
  394. int __mp_info(MP_CONST void *, __mp_allocinfo *);
  395. int __mp_printinfo(MP_CONST void *);
  396. void __mp_memorymap(int);
  397. void __mp_summary(void);
  398. void __mp_check(void);
  399. void (*__mp_prologue(void (*)(MP_CONST void *, size_t)))
  400.      (MP_CONST void *, size_t);
  401. void (*__mp_epilogue(void (*)(MP_CONST void *)))(MP_CONST void *);
  402. void (*__mp_nomemory(void (*)(void)))(void);
  403. void __mp_pushdelstack(MP_CONST char *, MP_CONST char *, unsigned long);
  404. void __mp_popdelstack(char **, char **, unsigned long *);
  405.  
  406.  
  407. #ifdef __cplusplus
  408. }
  409. #endif /* __cplusplus */
  410.  
  411. #else /* NDEBUG */
  412.  
  413. #define __mp_init() ((void) 0)
  414. #define __mp_fini() ((void) 0)
  415. #define __mp_alloc(l, a, f, s, t, u, k) ((void *) NULL)
  416. #define __mp_strdup(p, l, f, s, t, u, k) ((char *) NULL)
  417. #define __mp_realloc(p, l, a, f, s, t, u, k) ((void *) NULL)
  418. #define __mp_free(p, f, s, t, u, k) ((void) 0)
  419. #define __mp_setmem(p, l, c, f, s, t, u, k) ((void *) NULL)
  420. #define __mp_copymem(p, q, l, c, f, s, t, u, k) ((void *) NULL)
  421. #define __mp_locatemem(p, l, q, m, f, s, t, u, k) ((void *) NULL)
  422. #define __mp_comparemem(p, q, l, f, s, t, u, k) ((int) 0)
  423. #define __mp_info(p, d) ((int) 0)
  424. #define __mp_printinfo(p) ((int) 0)
  425. #define __mp_memorymap(s) ((void) 0)
  426. #define __mp_summary() ((void) 0)
  427. #define __mp_check() ((void) 0)
  428. #define __mp_prologue(h) ((void (*)(MP_CONST void *, size_t)) NULL)
  429. #define __mp_epilogue(h) ((void (*)(MP_CONST void *)) NULL)
  430. #define __mp_nomemory(h) ((void (*)(void)) NULL)
  431. #define __mp_pushdelstack(s, t, u) ((void) 0)
  432. #define __mp_popdelstack(s, t, u) ((void) 0)
  433.  
  434. #endif /* NDEBUG */
  435.  
  436.  
  437. #if !MP_NOCPLUSPLUS
  438. #ifdef __cplusplus
  439. #ifndef NDEBUG
  440.  
  441. typedef void (*new_handler)(void);
  442.  
  443.  
  444. #ifdef __GNUC__
  445. extern "C"
  446. {
  447. #endif /* __GNUC__ */
  448.  
  449.  
  450. /* Set the low-memory handler.
  451.  */
  452.  
  453. static inline new_handler set_new_handler(new_handler h)
  454. {
  455.     return __mp_nomemory(h);
  456. }
  457.  
  458.  
  459. #ifdef __GNUC__
  460. }
  461. #endif /* __GNUC__ */
  462.  
  463.  
  464. /* Override operator new.
  465.  */
  466.  
  467. static inline void *operator new(size_t l, MP_CONST char *s, MP_CONST char *t,
  468.                                  unsigned long u)
  469. {
  470.     return __mp_alloc(l, 0, MP_AT_NEW, s, t, u, 0);
  471. }
  472.  
  473.  
  474. /* Override operator new[].
  475.  */
  476.  
  477. static inline void *operator new[](size_t l, MP_CONST char *s, MP_CONST char *t,
  478.                                    unsigned long u)
  479. {
  480.     return __mp_alloc(l, 0, MP_AT_NEWVEC, s, t, u, 0);
  481. }
  482.  
  483.  
  484. /* Override operator delete.
  485.  */
  486.  
  487. static inline void operator delete(void *p)
  488. {
  489.     char *s, *t;
  490.     unsigned long u;
  491.  
  492.     __mp_popdelstack(&s, &t, &u);
  493.     __mp_free(p, MP_AT_DELETE, s, t, u, 0);
  494. }
  495.  
  496.  
  497. /* Override operator delete[].
  498.  */
  499.  
  500. static inline void operator delete[](void *p)
  501. {
  502.     char *s, *t;
  503.     unsigned long u;
  504.  
  505.     __mp_popdelstack(&s, &t, &u);
  506.     __mp_free(p, MP_AT_DELETEVEC, s, t, u, 0);
  507. }
  508.  
  509.  
  510. #define new ::new(MP_FUNCNAME, __FILE__, __LINE__)
  511. #define delete __mp_pushdelstack(MP_FUNCNAME, __FILE__, __LINE__), ::delete
  512.  
  513. #endif /* NDEBUG */
  514. #endif /* __cplusplus */
  515. #endif /* MP_NOCPLUSPLUS */
  516.  
  517.  
  518. #endif /* MP_MPATROL_H */
  519.